package com.oolatina.adapters;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import newspackage.AgendaBean;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cc.ImageLoader2;
import cc.Utils;

import com.oolatina.R;
import com.oolatina.activity.MainActivity;
import com.oolatina.fragments.AgendaFragment;

@SuppressLint("NewApi")
public class AgendaAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private ViewHolder holder;
	private String[] mLabels;
	private List<AgendaBean> mData;
	private AgendaFragment parent;
	ImageLoader2 imageLoader = null;
	Activity activity;
	
	public AgendaAdapter(Context c, List<AgendaBean> data, Activity a, AgendaFragment p) {
		mContext = c;
		mData = data;
		activity = a;
		parent = p;
		try {
			inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			imageLoader = new ImageLoader2(mContext.getApplicationContext());
		} catch (Exception e) {
		}
	}

	public void addItems(List<AgendaBean> beans) {
		mData.addAll(beans);
	}
	
	public int getCount() {
		return mData.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	static class ViewHolder {
		public ImageView image;
		public RelativeLayout bigTitle;
		public TextView title, ville, dayOfWeekBox, monthBox, dayBox, dateTitle;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			convertView = inflater.inflate(R.layout.agenda_detail_view, null);
			holder = new ViewHolder();
			holder.bigTitle = (RelativeLayout) convertView
					.findViewById(R.id.bigTitle);
			holder.dateTitle = (TextView) convertView
					.findViewById(R.id.dateTitle);
			holder.image = (ImageView) convertView.findViewById(R.id.NewsImage);
			holder.title = (TextView) convertView
					.findViewById(R.id.agendaTitle);
			holder.ville = (TextView) convertView
					.findViewById(R.id.agendaVille);
			holder.dayOfWeekBox = (TextView) convertView
					.findViewById(R.id.dayOfWeekBox);
			holder.monthBox = (TextView) convertView
					.findViewById(R.id.monthBox);
			holder.dayBox = (TextView) convertView
					.findViewById(R.id.dayBox);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mData.get(position).getThumbnail().length() > 10) {
			imageLoader.DisplayImage(mData.get(position).getThumbnail(),
					holder.image);
		}
		
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		int year = Utils.parseInt(mData.get(position).getYear());
		int month = Utils.parseInt(mData.get(position).getMonth());
		int day = Utils.parseInt(mData.get(position).getDay());
		calendar.set(year, month, day);
		holder.title.setText(mData.get(position).getTitle());
		holder.ville.setText(mData.get(position).getVille());
		holder.dayOfWeekBox.setText(calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()).substring(0, 3));
		holder.monthBox.setText(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()).substring(0, 3));
		holder.dayBox.setText(mData.get(position).getDay());
		
		if (position <= 0 || !mData.get(position - 1).getDay().equals(mData.get(position).getDay())
				|| !mData.get(position - 1).getMonth().equals(mData.get(position).getMonth())
				|| !mData.get(position - 1).getYear().equals(mData.get(position).getYear())) {
			
			holder.bigTitle.setVisibility(View.VISIBLE);
			holder.dateTitle.setText(
					mData.get(position).getDay()
					+ " " + calendar.getDisplayName(Calendar.MONDAY, Calendar.LONG, Locale.getDefault())
					+ " " + mData.get(position).getYear()
				);
			
		} else {
			holder.bigTitle.setVisibility(View.GONE);
		}
		
		// Lazy loading
//		if (mData.size() > 5 && position == mData.size() - 6) {
//			this.parent.addMoreItems();
//		}
		
		return convertView;
	}
	
}
